package com.icetec.iceintel.ui.activity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.icetec.R;
import com.icetec.iceintel.framework.HorizontalListView;
import com.icetec.iceintel.framework.IceBaseActivity;
import com.icetec.iceintel.ui.adapter.SchemeDeviceListAdapter;
import com.icetec.iceintel.ui.adapter.SchemeTypeAdapter;
import com.icetec.iceintel.utility.ToastUtility;

/**
 * Created by 坤道 on 2015/5/31.
 */
public class SchemeActivity extends IceBaseActivity{

    public static final String ID = "id";

    private TextView nameTv;
    private TextView backTv;
    private TextView editTv;
    private EditText renamEt;
    private Button deleteBt;
    private ListView devicelistLv;
    private TextView typeTv;
    private HorizontalListView typeLv;
    private SchemeTypeAdapter typeAdapter;
    private SchemeDeviceListAdapter deviceListAdapter;
    private boolean isEdit = false;
    private AlphaAnimation presentAnimation, fadeAnimation;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scheme);
        renamEt = (EditText) findViewById(R.id.act_scheme_remane_et);
        nameTv = (TextView) findViewById(R.id.act_scheme_name_tv);
        backTv = (TextView)findViewById(R.id.act_scheme_back_tv);
        editTv = (TextView)findViewById(R.id.act_scheme_modify_tv);
        deleteBt = (Button)findViewById(R.id.act_scheme_delete_bt);
        devicelistLv = (ListView)findViewById(R.id.act_scheme_devicelist_lv);
        typeTv = (TextView) findViewById(R.id.act_scheme_type_tv);
        typeLv = (HorizontalListView) findViewById(R.id.act_scheme_type_lv);

        typeAdapter = new SchemeTypeAdapter(this);
        typeLv.setAdapter(typeAdapter);

        typeLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                typeAdapter.setChoice(position);
                typeAdapter.updateImage(view);
            }
        });

        Bundle bundle = getIntent().getExtras();
        deviceListAdapter = new SchemeDeviceListAdapter(this, bundle.getInt(ID));
        deviceListAdapter.setIsEdited(isEdit);
        devicelistLv.setAdapter(deviceListAdapter);

        nameTv.setText(deviceListAdapter.getScheme().getName());

        presentAnimation= new AlphaAnimation(0f, 1.0f);
        presentAnimation.setDuration(500);

        fadeAnimation= new AlphaAnimation(1.0f, 0f);
        fadeAnimation.setDuration(500);

        findViewById(R.id.act_scheme_device_rely).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                findViewById(R.id.act_scheme_device_rely).setFocusable(true);
                findViewById(R.id.act_scheme_device_rely).setFocusableInTouchMode(true);
                findViewById(R.id.act_scheme_device_rely).requestFocus();
                HideKeyboard(v);
                return false;
            }
        });


        editTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isEdit) {
                    if (renamEt.getText().toString().equals("")) {
                        ToastUtility.show(R.string.errortips_empty);
                    }else {
                        nameTv.setText(renamEt.getText());
                        deviceListAdapter.getScheme().setType(typeAdapter.getChoiceType());
                        deviceListAdapter.getScheme().setName(renamEt.getText().toString());
                        deviceListAdapter.saveChangedOndb();
                        editTv.setText(R.string.modify);
                        backTv.setText(R.string.back);
                        deleteBt.setVisibility(View.VISIBLE);
                        isEdit = false;
                        HideKeyboard(v);
                        renamEt.setVisibility(View.GONE);
                        typeTv.setVisibility(View.GONE);
                        typeLv.setVisibility(View.GONE);
                        deviceListAdapter.setIsEdited(isEdit);
                    }
                } else {
                    typeAdapter.setChoice(deviceListAdapter.getScheme().getType());
                    typeAdapter.notifyDataSetChanged();
                    editTv.setText(R.string.ok);
                    backTv.setText(R.string.cancel);
                    deleteBt.setVisibility(View.GONE);
                    renamEt.setText(deviceListAdapter.getScheme().getName());
                    renamEt.setVisibility(View.VISIBLE);
                    typeTv.setVisibility(View.VISIBLE);
                    typeLv.setVisibility(View.VISIBLE);
                    renamEt.startAnimation(presentAnimation);
                    typeLv.startAnimation(presentAnimation);
                    typeTv.startAnimation(presentAnimation);
                    renamEt.requestFocus();
                    ShowKeyboard(v);
                    isEdit = true;
                    deviceListAdapter.setIsEdited(isEdit);
                    deviceListAdapter.notifyDataSetChanged();
                }
            }
        });

        deleteBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(SchemeActivity.this)
                        .setTitle(R.string.delete_schema)
                        .setMessage(R.string.is_delete_scheme)
                        .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                deviceListAdapter.deleteSchemeOndb();
                                SchemeActivity.this.finish();
                            }
                        })
                        .setNegativeButton(R.string.cancel, null).show();
            }
        });

        backTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isEdit){
                    deviceListAdapter.updateFromDb();
                    editTv.setText(R.string.modify);
                    backTv.setText(R.string.back);
                    HideKeyboard(v);
                    renamEt.setVisibility(View.GONE);
                    typeTv.setVisibility(View.GONE);
                    typeLv.setVisibility(View.GONE);
                    deleteBt.setVisibility(View.VISIBLE);
                    isEdit = false;
                    deviceListAdapter.setIsEdited(isEdit);
                    deviceListAdapter.notifyDataSetChanged();
                }else{
                    SchemeActivity.this.finish();
                }

            }
        });



    }

    //隐藏虚拟键盘
    public static void HideKeyboard(View v)
    {
        InputMethodManager imm = ( InputMethodManager ) v.getContext( ).getSystemService( Context.INPUT_METHOD_SERVICE );
        if ( imm.isActive( ) ) {
            imm.hideSoftInputFromWindow( v.getApplicationWindowToken( ) , 0 );

        }
    }

    //显示虚拟键盘
    public static void ShowKeyboard(View v)
    {
        InputMethodManager imm = ( InputMethodManager ) v.getContext( ).getSystemService( Context.INPUT_METHOD_SERVICE );

        imm.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT);

    }
}
